package com.cntcdt.api.db.dao.base;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cntcdt.api.db.model.AdminUserGroupModel;
import com.coola.jutil.sql.DBEngine;
import com.coola.jutil.sql.DBFactory;

public abstract class AdminUserGroupDaoBase {
	/**
	 * 日志处理类实例
	 */
	protected static Log logger = LogFactory.getLog(AdminUserGroupDaoBase.class);
	/**
	 * 只读DB引擎
	 */
	protected DBEngine readDBEngine = DBFactory.getDBEngine("main-statistic-b");
	/**
	 * 只写DB引擎
	 */
	protected DBEngine writeDBEngine = DBFactory.getDBEngine("main-statistic");
	/**
	 * admin_user_group 表
	 */
	protected static final String TABLE = "admin_user_group";

	/**
	 * 默认构造方法
	 */
	public AdminUserGroupDaoBase() {
	}

	/**
	 * 插入一条数据
	 * 
	 * @param model
	 * @return
	 */
	public int insert(AdminUserGroupModel model) {
		DBEngine localEngine = DBFactory.getKeepConnDBEngine("main-statistic");
		String sql = "INSERT INTO " + TABLE + " (groupName,addTime) VALUES (?,?)";
		Object[] paramObj = { model.getGroupName(), model.getAddTime() };
		int id = 0;
		try {
			id = localEngine.executeUpdate(sql, paramObj);
			ResultSet rs = localEngine.executeQuery("select last_insert_id() as id");
			if (rs.next()) {
				id = rs.getInt("id");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			localEngine.close();
		}
		return id;
	}

	/**
	 * 修改一条数据
	 * 
	 * @param model
	 * @return
	 */
	public boolean update(AdminUserGroupModel model) {
		String sql = "UPDATE  " + TABLE + " SET groupName = ? ,addTime = ? WHERE Id = ? ";
		Object[] paramObj = { model.getGroupName(), model.getAddTime(), model.getId() };
		try {
			return writeDBEngine.executeUpdate(sql, paramObj) > 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 按主键删除一条数据
	 * 
	 * @param id
	 * @return 删除成功返回true,否则返回false
	 */
	public boolean deleteByPrimaryKey(Integer id) {
		String sql = "DELETE FROM " + TABLE + " WHERE id = ?";
		Object[] paramObjs = { id };
		try {
			return writeDBEngine.executeUpdate(sql, paramObjs) > 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 获得model集合
	 * 
	 * @param sql
	 *            查询语句
	 * @return 返回Model集合
	 */
	protected List<AdminUserGroupModel> queryModelList(String sql) {
		List<AdminUserGroupModel> modelList = new ArrayList<AdminUserGroupModel>();
		try {
			ResultSet rs = readDBEngine.executeQuery(sql);
			while (rs.next()) {
				modelList.add(new AdminUserGroupModel(rs));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelList;
	}

	/**
	 * 获得model集合
	 * 
	 * @param sql
	 *            查询语句
	 * @param objs
	 *            参数集
	 * @return 返回Model集合
	 */
	protected List<AdminUserGroupModel> queryModelList(String sql, Object[] paramObjs) {
		List<AdminUserGroupModel> modelList = new ArrayList<AdminUserGroupModel>();
		try {
			ResultSet rs = readDBEngine.executeQuery(sql, paramObjs);
			while (rs.next()) {
				modelList.add(new AdminUserGroupModel(rs));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelList;
	}

	/**
	 * 按主键查询model
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public AdminUserGroupModel findByPrimaryKey(Integer id) {
		String sql = "SELECT * FROM " + TABLE + " WHERE id = ?";
		Object[] paramObjs = { id };
		List<AdminUserGroupModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList != null && modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	/**
	 * 获得model集合
	 * 
	 * @return 返回Model集合
	 */
	public List<AdminUserGroupModel> findAll() {
		String sql = "SELECT * FROM " + TABLE;
		return queryModelList(sql);
	}

}
